package com.xinfu.newproject.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.meter.bean.AddUserMeterBean;
import com.xinfu.newproject.bean.meter.bean.CheckMeterCodeBean;
import com.xinfu.newproject.bean.meter.bean.MeterListBean;
import com.xinfu.newproject.presenter.MeterPresenter;
import com.xinfu.newproject.adapter.meter.ElectricityRefillPullToRefreshAdapter;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class ElectricityRefillActivity extends BaseActivity implements IView.IMeterView {

    @BindView(R.id.activity_electricity_refill_srl)
    SwipeRefreshLayout mSwipeRefreshLayout;

    @BindView(R.id.activity_electricity_refill_rv)
    RecyclerView mRecyclerView;

    @BindView(R.id.activity_electricity_add_meter_tv)
    TextView addTv;

    private ElectricityRefillPullToRefreshAdapter mAdapter;

    private MeterPresenter meterPresenter = new MeterPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_electricity_refill);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("电费充值");
        initViewBind();
        initLisener();
    }

    String[] items = null;
    List<String> amountList = new ArrayList<>();
    private void initLisener() {
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.item_electricity_refill_tv_amount:
                        amountList.clear();
                        for (int i = 0; i < meterListBean.getResult().get(position).getCharges().size(); i++) {
                            amountList.add(meterListBean.getResult().get(position).getCharges().get(i).getCharges());
                        }
                        items = (String[]) amountList.toArray(new String[amountList.size()]);

                        if (items != null) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(ElectricityRefillActivity.this);
                            builder.setIcon(android.R.drawable.alert_dark_frame);
                            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    mAdapter.addAmount(position, items[which]);
                                    dialog.dismiss();
                                }
                            });
                            builder.show();
                        }
                        break;
                    case R.id.item_electricity_refill_tv_recharge:
                        if (isLogin()) {
                            meterPresenter.meterPay();
                        }
                        break;
                }
            }
        });
    }

    private void initViewBind() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ElectricityRefillPullToRefreshAdapter();
//        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
//            @Override
//            public void onLoadMoreRequested() {
//                loadMore();
//            }
//        });
        mRecyclerView.setAdapter(mAdapter);
        initRefreshLayout();
        mSwipeRefreshLayout.setRefreshing(true);
        refresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    @OnClick(R.id.activity_electricity_add_meter_tv)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_electricity_add_meter_tv:
                Bundle bundle = new Bundle();
                bundle.putString("title", "添加电表");
                bundle.putString("type", getIntent().getExtras().getString("type"));
                startIntentActivity(AddElectricWaterMetterActivity.class, bundle);
                break;
        }
    }

    /**
     * 加载更多
     */
    private void loadMore() {

    }

    /**
     * 刷新数据
     */
    private void refresh() {
        if (isLogin()) {
            meterPresenter.meterList();
        }
    }

    /**
     * 下拉刷新
     */
    private void initRefreshLayout() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            return false;
        }


        return true;
    }

    @Override
    public String header() {
        return null;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String meterCode() {
        return null;
    }

    @Override
    public String meterType() {
        return getIntent().getExtras().getString("type");
    }

    @Override
    public String projectId() {
        return null;
    }

    @Override
    public String meterId() {
        return null;
    }

    @Override
    public void showToast(String content) {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void getData(AddUserMeterBean addUserMeterBean) {

    }

    @Override
    public void getData(CheckMeterCodeBean checkMeterCodeBean) {

    }

    MeterListBean meterListBean;

    @Override
    public void getData(MeterListBean meterListBean) {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
        mAdapter.setNewData(meterListBean.getResult());
        this.meterListBean = meterListBean;
    }
}
